import os
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

# 定义一个函数，用于设置文档中图片的环绕方式
def set_image_wrap_style(doc, wrap_style):
    for paragraph in doc.paragraphs:

        if paragraph.runs:
            for run in paragraph.runs:
                print(run)
                if run._element.tag.endswith('}r'):
                    if run._element.find('.//wp:docPr', namespaces=run._element.nsmap) is not None:
                        # 如果是图片
                        run._element.find('.//wp:wrap', namespaces=run._element.nsmap).set('type', wrap_style)

# 获取当前文件夹下的所有 Word 文档
folder_path = '.'  # 当前文件夹路径
doc_files = [f for f in os.listdir(folder_path) if f.endswith('.docx')]

# 遍历每个 Word 文档并处理图片环绕方式
for doc_file in doc_files:
    doc = Document(os.path.join(folder_path, doc_file))
    set_image_wrap_style(doc, 'square')
    doc.save(os.path.join(folder_path, 'output_' + doc_file))
